package com.distribution.system.controller;

import cn.hutool.core.util.ObjectUtil;
import com.distribution.common.core.controller.BaseController;
import com.distribution.common.core.domain.AjaxResult;
import com.distribution.common.utils.StringUtils;
import com.distribution.system.domain.entity.ImgType;
import com.distribution.system.domain.req.ImgTypeReq;
import com.distribution.system.service.ImgTypeService;
import com.distribution.system.utils.convert.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author: GodLu
 * @create: 2024-11-09 15:10
 * @description: 图片分类控制器
 */
@Slf4j
@RestController
@RequestMapping("/imgType")
public class ImgTypeController extends BaseController {
    @Resource
    private ImgTypeService imgTypeService;

    @GetMapping("/getImgTypeList")
    public AjaxResult getImgTypeList(){
        List<ImgType> list = imgTypeService.lambdaQuery().orderByAsc(ImgType::getSort).list();
        return success(list);
    }

    @GetMapping("/getImgTypeListByName/{typeName}")
    public AjaxResult getImgTypeListByName(@PathVariable String typeName){
        List<ImgType> list = imgTypeService.lambdaQuery()
                .like(StringUtils.isNotBlank(typeName), ImgType::getTypeName, "%" + typeName + "%")
                .orderByAsc(ImgType::getSort).list();
        return success(list);
    }

    @Transactional
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ImgTypeReq imgTypeReq){
        ImgType imgType = new ImgType();
        BeanUtils.copyBean(imgTypeReq, imgType);
        List<ImgType> geList = imgTypeService.lambdaQuery().ge(ImgType::getSort, imgTypeReq.getSort()).list();
        geList.forEach(f -> {
            f.setSort(f.getSort() + 1);
        });
        geList.add(imgType);
        return imgTypeService.saveOrUpdateBatch(geList) ? success() : error("添加失败");
    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable Integer id){
        return imgTypeService.removeById(id) ? success() : error("删除失败");
    }

    @Transactional
    @PostMapping("/updateById")
    public AjaxResult updateById(@RequestBody ImgTypeReq imgTypeReq){
        if (ObjectUtil.isNotNull(imgTypeReq.getSort())){
            List<ImgType> geList = imgTypeService.lambdaQuery().ge(ImgType::getSort, imgTypeReq.getSort()).list();
            geList.forEach(f -> {
                f.setSort(f.getSort() + 1);
            });
            imgTypeService.saveOrUpdateBatch(geList);
        }
        boolean updated = imgTypeService.lambdaUpdate().eq(ImgType::getId, imgTypeReq.getId())
                .set(StringUtils.isNotBlank(imgTypeReq.getTypeName()), ImgType::getTypeName, imgTypeReq.getTypeName())
                .set(ObjectUtil.isNotNull(imgTypeReq.getSort()), ImgType::getSort, imgTypeReq.getSort()).update();
        return updated ? success() : error("更新失败");
    }
}
